//
//  UIColor+Hex.m
//  DigitalScholl
//
//  Created by rachel on 15/1/8.
//  Copyright (c) 2015年 刘军林. All rights reserved.
//

#import "UIColor+Hex.h"

@implementation UIColor(Hex)

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return MRGBA_COLOR((float)r, (float)g, (float)b, alpha);
}

+ (UIColor *)colorWithInteger:(NSInteger )colorInteger alpha:(CGFloat)alpha
{
    
//    Byte *rByte = (Byte *)malloc(2);
//    Byte *gByte = (Byte *)malloc(2);
//    Byte *bByte = (Byte *)malloc(2);
    
    NSInteger b = colorInteger & 0xff;
    NSInteger g = (colorInteger>>8) & 0xff;
    NSInteger r = (colorInteger>>16) & 0xff;
    
    return MRGBA_COLOR((float)r, (float)g, (float)b, alpha);
}

+(NSString *) RGBStringWithColor:(UIColor *)color
{
    NSMutableString *rgbString = [[NSMutableString alloc] initWithString:@""];
    
    NSString *RGBStr = nil;
    //获得RGB值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",color];
    //将RGB值描述分隔成字符串
    NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:RGBArr];
    [array removeObjectAtIndex:RGBArr.count-1];
    [array removeObjectAtIndex:0];
    
    //获取红色值
    int r = 0;
    if (array.count > 0) {
        r = [[RGBArr objectAtIndex:1] floatValue] * 255;
    }
    if (r<16) {
        RGBStr = [NSString stringWithFormat:@"0%x",r];
    }else{
        RGBStr = [NSString stringWithFormat:@"%x",r];
    }
    [rgbString appendString:RGBStr];
    
    //获取绿色值
    int g = 0;
    if (array.count > 1) {
        g = [[RGBArr objectAtIndex:1] floatValue] * 255;
    }
    if (g<16) {
        RGBStr = [NSString stringWithFormat:@"0%x",g];
    }else {
        RGBStr = [NSString stringWithFormat:@"%x",g];
    }
    [rgbString appendString:RGBStr];
    
    //获取蓝色值
    int b = 0;
    if (array.count > 2) {
        b = [[RGBArr objectAtIndex:2] floatValue] * 255;
    }
    if (b<16) {
        RGBStr = [NSString stringWithFormat:@"0%x",b];
    }else {
        RGBStr = [NSString stringWithFormat:@"%x",b];
    }
    [rgbString appendString:RGBStr];
    //返回保存RGB值的数组
    
    return rgbString;
}


@end
